# -*- coding:utf-8 -*-
import time
from functools import wraps
import arcpy

def str_to_bool(s):
    if not isinstance(s, basestring):  # Python 2 中检查是否为字符串或unicode
        raise ValueError("Input must be a string")

    s = s.strip().lower()
    mapping = {
        'true': True,
        't': True,
        'yes': True,
        'y': True,
        '1': True,
        'false': False,
        'f': False,
        'no': False,
        'n': False,
        '0': False
    }
    return mapping.get(s)  # 如果s不在字典中，返回None
    # --- 使用示例 ---
    # print str_to_bool("True")  # 输出: True
    # print str_to_bool("  no  ")  # 输出: False
    # print str_to_bool("1")  # 输出: True
    # print str_to_bool("F")  # 输出: False
    # print str_to_bool("maybe")  # 输出: None

def check_lock_with_testschemalock(data_path):
    """使用 TestSchemaLock 检查数据是否被锁定"""
    try:
        if arcpy.TestSchemaLock(data_path):
            arcpy.AddMessage("数据 '{}' 未被锁定，可以进行修改。".format(data_path))
            return True
        else:
            arcpy.AddWarning("数据 '{}' 已被锁定，无法修改。".format(data_path))
            return False
    except Exception as e:
        arcpy.AddError("检查锁时发生错误: {}".format(str(e)))
        return False
def timer(func):
    @wraps(func)  # 这将保留被装饰函数的原始名称和文档
    def call_func(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        spend_seconds = end_time - start_time
        arcpy.AddMessage('完成函数{0}的调用，耗时如下：'.format(func.__name__))
        if spend_seconds < 60:
            arcpy.AddMessage("\tspend time: {0:0.2f} seconds".format(spend_seconds))
        else:
            hours = spend_seconds // 3600
            left_seconds = spend_seconds % 3600
            mins = left_seconds // 60
            left_seconds2 = left_seconds % 60
            arcpy.AddMessage("\tspend time: {0} hours {1} minutes {2:0.0f} seconds".format(hours, mins, left_seconds2))
            arcpy.AddMessage("\tspend time: {0:0.2f} seconds".format(spend_seconds))
        arcpy.AddMessage('')
        return result
    return call_func

if __name__ == '__main__':
    pass
